package org.wukl.vhr.controller.system.basic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wukl.vhr.model.Menu;
import org.wukl.vhr.model.ResponseBean;
import org.wukl.vhr.model.Role;
import org.wukl.vhr.service.MenuService;
import org.wukl.vhr.service.RoleService;

import java.util.List;

@RestController
@RequestMapping("/sys/basic/permession")
public class PermessionController {
    @Autowired
    RoleService roleService;
    @Autowired
    MenuService menuService;
    @GetMapping("/")
    public List<Role> getAllRoles()
    {
        return roleService.getAllRoles();
    }
    @GetMapping("/initAllMenus")
    public List<Menu> getAllMenus()
    {
        return menuService.getAllMenus();
    }
    @GetMapping("/getMenuIds/{rid}")
    public List<Integer> getMenuIdsByRoleId(@PathVariable Integer rid)
    {
        return menuService.getMenuIdsByRoleId(rid);
    }
    @PutMapping("/")
    public ResponseBean updateMenuIdsByRoleId(Integer rid,Integer[] mids)
    {

            if(menuService.updateMenuByRoleId(rid,mids))
            {
                return ResponseBean.responseSuccess("角色访问权限更新成功");
            }
            return ResponseBean.responseError("角色访问权限更新失败");
    }
    @PostMapping("/addRole")
    public ResponseBean addRole(@RequestBody Role role)
    {
        if(roleService.addRole(role)==1)
        {
            return ResponseBean.responseSuccess("角色添加成功");
        }
        return ResponseBean.responseError("角色添加失败");
    }
    @DeleteMapping("/role/{rid}")
    public ResponseBean deleteRoleById(@PathVariable Integer rid)
    {
        if(roleService.deleteRoleById(rid)==1)
        {
            return ResponseBean.responseSuccess("角色删除成功");
        }
        return ResponseBean.responseError("角色删除失败");
    }
}
